/*
 * Copyright (C) 2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

/*
 * Prototypes
 */
#if PROTO
static void
cp_rem_c_cp(c_t *u, c_t v, c_t *w, eint32_t m);

static void
cp_rem_cp_c(c_t *u, c_t *v, c_t w, eint32_t m);

static void
cp_rem_cp_cp(c_t *u, c_t *v, c_t *w, eint32_t m);

static void
sp_rem_s_sp(s_t *u, s_t v, s_t *w, eint32_t m);

static void
sp_rem_sp_s(s_t *u, s_t *v, s_t w, eint32_t m);

static void
sp_rem_sp_sp(s_t *u, s_t *v, s_t *w, eint32_t m);

static void
ip_rem_i_ip(i_t *u, i_t v, i_t *w, eint32_t m);

static void
ip_rem_ip_i(i_t *u, i_t *v, i_t w, eint32_t m);

static void
ip_rem_ip_ip(i_t *u, i_t *v, i_t *w, eint32_t m);

static void
lp_rem_l_lp(l_t *u, l_t v, l_t *w, eint32_t m);

static void
lp_rem_lp_l(l_t *u, l_t *v, l_t w, eint32_t m);

static void
lp_rem_lp_lp(l_t *u, l_t *v, l_t *w, eint32_t m);

static void
fp_rem_f_fp(f_t *u, f_t v, f_t *w, eint32_t m);

static void
fp_rem_fp_f(f_t *u, f_t *v, f_t w, eint32_t m);

static void
fp_rem_fp_fp(f_t *u, f_t *v, f_t *w, eint32_t m);

static void
dp_rem_d_dp(d_t *u, d_t v, d_t *w, eint32_t m);

static void
dp_rem_dp_d(d_t *u, d_t *v, d_t w, eint32_t m);

static void
dp_rem_dp_dp(d_t *u, d_t *v, d_t *w, eint32_t m);

static void
vo_rem_i_vo(vo_t *u, l_t v, vo_t *w, eint32_t m);

static void
vo_rem_vo_i(vo_t *u, vo_t *v, l_t w, eint32_t m);

static void
vo_rem_d_vo(vo_t *u, d_t v, vo_t *w, eint32_t m);

static void
vo_rem_vo_d(vo_t *u, vo_t *v, d_t w, eint32_t m);

static void
vo_rem_z_vo(vo_t *u, empz_t v, vo_t *w, eint32_t m);

static void
vo_rem_vo_z(vo_t *u, vo_t *v, empz_t w, eint32_t m);

static void
vo_rem_q_vo(vo_t *u, empq_t v, vo_t *w, eint32_t m);

static void
vo_rem_vo_q(vo_t *u, vo_t *v, empq_t w, eint32_t m);

static void
vo_rem_r_vo(vo_t *u, empr_t v, vo_t *w, eint32_t m);

static void
vo_rem_vo_r(vo_t *u, vo_t *v, empr_t w, eint32_t m);

static void
vo_rem_vo_vo(vo_t *u, vo_t *v, vo_t *w, eint32_t m);

static void
o_rem_o_o(o_t *u, o_t *v, o_t *w);
#endif

/*
 * Implementation
 */
#if CODE
void
evector_int_rem(eint_t a, evector_t *b, etype_t t)
{
    eint32_t	 l;
    evector_t	*v;
    etype_t	 vt;
    v_enter();

    l = b->length;
    v_check(2);
    if (a > 2147483647 || a < -2147483647)
	vt = match(t_int64, t, 0, 0);
    else if (a > 32767 || a < -32767)
	vt = match(t_int32, t, 0, 0);
    else if (a > 127 || a < -127)
	vt = match(t_int16, t, 0, 0);
    else
	vt = match(t_int8, t, 0, 0);
    if (vt != t)
	b = coerce(b, t, vt);
    v = create(b, vt);
    switch (vt) {
	case t_int8:
	    cp_rem_c_cp(v->v.obj, a, b->v.obj, l);
	    break;
	case t_int16:
	    sp_rem_s_sp(v->v.obj, a, b->v.obj, l);
	    break;
	case t_int32:
	    ip_rem_i_ip(v->v.obj, a, b->v.obj, l);
	    break;
	case t_int64:
	    lp_rem_l_lp(v->v.obj, a, b->v.obj, l);
	    break;
	case t_float32:
	    fp_rem_f_fp(v->v.obj, a, b->v.obj, l);
	    break;
	case t_float64:
	    dp_rem_d_dp(v->v.obj, a, b->v.obj, l);
	    break;
	default:
	    vo_rem_i_vo(v->v.obj, a, b->v.obj, l);
	    break;
    }
    v_leave();
}

void
evector_rem_int(evector_t *a, eint_t b, etype_t t)
{
    eint32_t	 l;
    evector_t	*v;
    etype_t	 vt;
    v_enter();

    l = a->length;
    v_check(2);
    if (b > 2147483647 || b < -2147483647)
	vt = match(t_int64, t, 0, 0);
    else if (b > 32767 || b < -32767)
	vt = match(t_int32, t, 0, 0);
    else if (b > 127 || b < -127)
	vt = match(t_int16, t, 0, 0);
    else
	vt = match(t_int8, t, 0, 0);
    if (vt != t)
	a = coerce(a, t, vt);
    v = create(a, vt);
    switch (vt) {
	case t_int8:
	    cp_rem_cp_c(v->v.obj, a->v.obj, b, l);
	    break;
	case t_int16:
	    sp_rem_sp_s(v->v.obj, a->v.obj, b, l);
	    break;
	case t_int32:
	    ip_rem_ip_i(v->v.obj, a->v.obj, b, l);
	    break;
	case t_int64:
	    lp_rem_lp_l(v->v.obj, a->v.obj, b, l);
	    break;
	case t_float32:
	    fp_rem_fp_f(v->v.obj, a->v.obj, b, l);
	    break;
	case t_float64:
	    dp_rem_dp_d(v->v.obj, a->v.obj, b, l);
	    break;
	default:
	    vo_rem_vo_i(v->v.obj, a->v.obj, b, l);
	    break;
    }
    v_leave();
}

void
evector_float_rem(efloat_t a, evector_t *b, etype_t t)
{
    eint32_t	 l;
    evector_t	*v;
    etype_t	 vt;
    v_enter();

    l = b->length;
    v_check(2);
    vt = match(t_float, t, 0, 0);
    if (vt != t)
	b = coerce(b, t, vt);
    v = create(b, vt);
    switch (vt) {
	case t_float64:
	    dp_rem_d_dp(v->v.obj, a, b->v.obj, l);
	    break;
	default:
	    vo_rem_d_vo(v->v.obj, a, b->v.obj, l);
	    break;
    }
    v_leave();
}

void
evector_rem_float(evector_t *a, efloat_t b, etype_t t)
{
    eint32_t	 l;
    evector_t	*v;
    etype_t	 vt;
    v_enter();

    l = a->length;
    v_check(2);
    vt = match(t_float, t, 0, 0);
    if (vt != t)
	a = coerce(a, t, vt);
    v = create(a, vt);
    switch (vt) {
	case t_float64:
	    dp_rem_dp_d(v->v.obj, a->v.obj, b, l);
	    break;
	default:
	    vo_rem_vo_d(v->v.obj, a->v.obj, b, l);
	    break;
    }
    v_leave();
}

void
evector_mpz_rem(empz_t a, evector_t *b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	b = coerce(b, t, t_void);
    v = create(b, t_void);
    vo_rem_z_vo(v->v.obj, a, b->v.obj, v->length);
    v_leave();
}

void
evector_rem_mpz(evector_t *a, empz_t b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	a = coerce(a, t, t_void);
    v = create(a, t_void);
    vo_rem_vo_z(v->v.obj, a->v.obj, b, v->length);
    v_leave();
}

void
evector_mpq_rem(empq_t a, evector_t *b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	b = coerce(b, t, t_void);
    v = create(b, t_void);
    vo_rem_q_vo(v->v.obj, a, b->v.obj, v->length);
    v_leave();
}

void
evector_rem_mpq(evector_t *a, empq_t b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	a = coerce(a, t, t_void);
    v = create(a, t_void);
    vo_rem_vo_q(v->v.obj, a->v.obj, b, v->length);
    v_leave();
}

void
evector_mpr_rem(empr_t a, evector_t *b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	b = coerce(b, t, t_void);
    v = create(b, t_void);
    vo_rem_r_vo(v->v.obj, a, b->v.obj, v->length);
    v_leave();
}

void
evector_rem_mpr(evector_t *a, empr_t b, etype_t t)
{
    evector_t	*v;
    v_enter();

    v_check(2);
    if (t != t_void)
	a = coerce(a, t, t_void);
    v = create(a, t_void);
    vo_rem_vo_r(v->v.obj, a->v.obj, b, v->length);
    v_leave();
}

void
evector_rem(evector_t *a, evector_t *b, etype_t at, etype_t bt)
{
    eint32_t	l;
    evop_t	o;
    v_enter();

    setup(&o, a, b, at, bt, 0, 0);
    l = o.r->length;
    switch (o.t) {
	case t_int8:
	    cp_rem_cp_cp(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_int16:
	    sp_rem_sp_sp(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_int32:
	    ip_rem_ip_ip(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_int64:
	    lp_rem_lp_lp(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_float32:
	    fp_rem_fp_fp(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	case t_float64:
	    dp_rem_dp_dp(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
	default:
	    vo_rem_vo_vo(o.r->v.obj, o.a->v.obj, o.b->v.obj, l);
	    break;
    }
    v_leave();
}

static void
cp_rem_c_cp(c_t *u, c_t v, c_t *w, eint32_t m)
{
    while (m--)
	*u++ = v % *w++;
}

static void
cp_rem_cp_c(c_t *u, c_t *v, c_t w, eint32_t m)
{
    while (m--)
	*u++ = *v++ % w;
}

static void
cp_rem_cp_cp(c_t *u, c_t *v, c_t *w, eint32_t m)
{
    while (m--)
	*u++ = *v++ % *w++;
}

static void
sp_rem_s_sp(s_t *u, s_t v, s_t *w, eint32_t m)
{
    while (m--)
	*u++ = v % *w++;
}

static void
sp_rem_sp_s(s_t *u, s_t *v, s_t w, eint32_t m)
{
    while (m--)
	*u++ = *v++ % w;
}

static void
sp_rem_sp_sp(s_t *u, s_t *v, s_t *w, eint32_t m)
{
    while (m--)
	*u++ = *v++ % *w++;
}

static void
ip_rem_i_ip(i_t *u, i_t v, i_t *w, eint32_t m)
{
    while (m--)
	*u++ = v % *w++;
}

static void
ip_rem_ip_i(i_t *u, i_t *v, i_t w, eint32_t m)
{
    while (m--)
	*u++ = *v++ % w;
}

static void
ip_rem_ip_ip(i_t *u, i_t *v, i_t *w, eint32_t m)
{
    while (m--)
	*u++ = *v++ % *w++;
}

static void
lp_rem_l_lp(l_t *u, l_t v, l_t *w, eint32_t m)
{
    while (m--)
	*u++ = v % *w++;
}

static void
lp_rem_lp_l(l_t *u, l_t *v, l_t w, eint32_t m)
{
    while (m--)
	*u++ = *v++ % w;
}

static void
lp_rem_lp_lp(l_t *u, l_t *v, l_t *w, eint32_t m)
{
    while (m--)
	*u++ = *v++ % *w++;
}

static void
fp_rem_f_fp(f_t *u, f_t v, f_t *w, eint32_t m)
{
    while (m--)
	*u++ = fmodf(v, *w++);
}

static void
fp_rem_fp_f(f_t *u, f_t *v, f_t w, eint32_t m)
{
    while (m--)
	*u++ = fmodf(*v++, w);
}

static void
fp_rem_fp_fp(f_t *u, f_t *v, f_t *w, eint32_t m)
{
    while (m--)
	*u++ = fmodf(*v++, *w++);
}

static void
dp_rem_d_dp(d_t *u, d_t v, d_t *w, eint32_t m)
{
    while (m--)
	*u++ = fmod(v, *w++);
}

static void
dp_rem_dp_d(d_t *u, d_t *v, d_t w, eint32_t m)
{
    while (m--)
	*u++ = fmod(*v++, w);
}

static void
dp_rem_dp_dp(d_t *u, d_t *v, d_t *w, eint32_t m)
{
    while (m--)
	*u++ = fmod(*v++, *w++);
}

static void
vo_rem_i_vo(vo_t *u, l_t v, vo_t *w, eint32_t m)
{
    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		eraise_exception(except_floating_point_error);
		break;
	    case t_int:
		set_i(v % get_i(w));
		break;
	    case t_float:
		set_d(fmod(v, get_d(w)));
		break;
	    case t_mpz:
		if (ez_rem_i_z(thr_zr, v, get_z(w)))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		switch (eq_rem_i_q(thr_qr, v, get_q(w))) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_z(thr_zr);
			break;
		}
		break;
	    case t_mpr:
		er_rem_i_r(thr_rr, v, get_r(w));
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_rem_vo_i(vo_t *u, vo_t *v, l_t w, eint32_t m)
{
    eint_t	i;

    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		if (w == 0)
		    eraise_exception(except_floating_point_error);
		set_i(0);
		break;
	    case t_int:
		set_i(get_i(v) % w);
		break;
	    case t_float:
		set_d(fmod(get_d(v), w));
		break;
	    case t_mpz:
		if ((i = ez_rem_z_i(thr_zr, get_z(v), w)) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		switch (eq_rem_q_i(thr_qr, get_q(v), w)) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_z(thr_zr);
			break;
		}
		break;
	    case t_mpr:
		er_rem_r_i(thr_rr, get_r(v), w);
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_rem_d_vo(vo_t *u, d_t v, vo_t *w, eint32_t m)
{
    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		/* FIXME only nan as result... */
		set_d(fmod(v, 0.0));
		break;
	    case t_int:
		set_d(fmod(v, get_i(w)));
		break;
	    case t_float:
		set_d(fmod(v, get_d(w)));
		break;
	    case t_mpz:
		set_d(fmod(v, mpz_get_d(get_z(w))));
		break;
	    case t_mpq:
		set_d(fmod(v, mpq_get_d(get_q(w))));
		break;
	    case t_mpr:
		er_rem_d_r(thr_rr, v, get_r(w));
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_rem_vo_d(vo_t *u, vo_t *v, d_t w, eint32_t m)
{
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		/* FIXME result is zero or nan */
		set_d(fmod(0.0, w));
		break;
	    case t_int:
		set_d(fmod(get_i(v), w));
		break;
	    case t_float:
		set_d(fmod(get_d(v), w));
		break;
	    case t_mpz:
		set_d(fmod(mpz_get_d(get_z(v)), w));
		break;
	    case t_mpq:
		set_d(fmod(mpq_get_d(get_q(v)), w));
		break;
	    case t_mpr:
		er_rem_r_d(thr_rr, get_r(v), w);
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
}

static void
vo_rem_z_vo(vo_t *u, empz_t v, vo_t *w, eint32_t m)
{
    eint_t	i;
    ebool_t	b;

    if (unlikely((b = v == thr_zr))) {
	++thread_self->esp;
	eget_mpz(v, (thread_self->esp - thread_self->ess) - 1);
	v = thread_self->esp[-1].v.o;
    }
    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		eraise_exception(except_floating_point_error);
		break;
	    case t_int:
		if ((i = ez_rem_z_i(thr_zr, v, get_i(w))) != MININT)
		    set_i(i);
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		set_d(fmod(mpz_get_d(v), get_d(w)));
		break;
	    case t_mpz:
		if (ez_rem_z_z(thr_zr, v, get_z(w)))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		switch (eq_rem_z_q(thr_qr, v, get_q(w))) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_z(thr_zr);
			break;
		}
		break;
	    case t_mpr:
		er_rem_z_r(thr_rr, v, get_r(w));
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_rem_vo_z(vo_t *u, vo_t *v, empz_t w, eint32_t m)
{
    ebool_t	b;

    if (unlikely((b = w == thr_zr))) {
	++thread_self->esp;
	eget_mpz(w, (thread_self->esp - thread_self->ess) - 1);
	w = thread_self->esp[-1].v.o;
    }
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		set_i(0);
		break;
	    case t_int:
		if (ez_rem_i_z(thr_zr, get_i(v), w))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_float:
		set_d(fmod(get_d(v), mpz_get_d(w)));
		break;
	    case t_mpz:
		if (ez_rem_z_z(thr_zr, get_z(v), w))
		    set_i(empz_get_i(thr_zr));
		else
		    set_z(thr_zr);
		break;
	    case t_mpq:
		switch (eq_rem_q_z(thr_qr, get_q(v), w)) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_z(thr_zr);
			break;
		}
		break;
	    case t_mpr:
		er_rem_r_z(thr_rr, get_r(v), w);
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_rem_q_vo(vo_t *u, empq_t v, vo_t *w, eint32_t m)
{
    ebool_t	b;

    if (unlikely((b = v == thr_qr))) {
	++thread_self->esp;
	eget_mpq(v, (thread_self->esp - thread_self->ess) - 1);
	v = thread_self->esp[-1].v.o;
    }
    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		eraise_exception(except_floating_point_error);
		break;
	    case t_int:
		switch (eq_rem_q_i(thr_qr, v, get_i(w))) {
		    case 0:
			set_q(thr_qr);
			break;
		    case -1:
			set_z(thr_zr);
			break;
		    default:
			set_i(empz_get_i(thr_zr));
			break;
		}
		break;
	    case t_float:
		set_d(fmod(mpq_get_d(v), get_d(w)));
		break;
	    case t_mpz:
		switch (eq_rem_q_z(thr_qr, v, get_z(w))) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_z(thr_zr);
			break;
		}
		break;
	    case t_mpq:
		switch (eq_rem_q_q(thr_qr, v, get_q(w))) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_z(thr_zr);
			break;
		}
		break;
	    case t_mpr:
		er_rem_q_r(thr_rr, v, get_r(w));
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_rem_vo_q(vo_t *u, vo_t *v, empq_t w, eint32_t m)
{
    ebool_t	b;

    if (unlikely((b = w == thr_qr))) {
	++thread_self->esp;
	eget_mpq(w, (thread_self->esp - thread_self->ess) - 1);
	w = thread_self->esp[-1].v.o;
    }
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		set_i(0);
		break;
	    case t_int:
		switch (eq_rem_i_q(thr_qr, get_i(v), w)) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case -1:
			set_z(thr_zr);
			break;
		    default:
			set_q(thr_qr);
			break;
		}
		break;
	    case t_float:
		set_d(fmod(get_d(v), mpq_get_d(w)));
		break;
	    case t_mpz:
		switch (eq_rem_z_q(thr_qr, get_z(v), w)) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_z(thr_zr);
			break;
		}
		break;
	    case t_mpq:
		switch (eq_rem_q_q(thr_qr, get_q(v), w)) {
		    case 1:
			set_i(empz_get_i(thr_zr));
			break;
		    case 0:
			set_q(thr_qr);
			break;
		    default:
			set_z(thr_zr);
			break;
		}
		break;
	    case t_mpr:
		er_rem_r_q(thr_rr, get_r(v), w);
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_rem_r_vo(vo_t *u, empr_t v, vo_t *w, eint32_t m)
{
    ebool_t	b;

    if (unlikely((b = v == thr_rr))) {
	++thread_self->esp;
	eget_mpr(v, (thread_self->esp - thread_self->ess) - 1);
	v = thread_self->esp[-1].v.o;
    }
    for (; m; u++, w++, m--) {
	switch (eobject_type(*w)) {
	    case t_void:
		/* FIXME only nan as result */
		er_rem_r_i(thr_rr, v, 0.0);
		set_r(thr_rr);
		break;
	    case t_int:
		er_rem_r_i(thr_rr, v, get_i(w));
		set_r(thr_rr);
		break;
	    case t_float:
		er_rem_r_d(thr_rr, v, get_d(w));
		set_r(thr_rr);
		break;
	    case t_mpz:
		er_rem_r_z(thr_rr, v, get_z(w));
		set_r(thr_rr);
		break;
	    case t_mpq:
		er_rem_r_q(thr_rr, v, get_q(w));
		set_r(thr_rr);
		break;
	    case t_mpr:
		er_rem_r_r(thr_rr, v, get_r(w));
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_rem_vo_r(vo_t *u, vo_t *v, empr_t w, eint32_t m)
{
    ebool_t	b;

    if (unlikely((b = w == thr_rr))) {
	++thread_self->esp;
	eget_mpr(w, (thread_self->esp - thread_self->ess) - 1);
	w = thread_self->esp[-1].v.o;
    }
    for (; m; u++, v++, m--) {
	switch (eobject_type(*v)) {
	    case t_void:
		/* FIXME result is zero or nan */
		er_rem_i_r(thr_rr, 0, w);
		set_r(thr_rr);
		break;
	    case t_int:
		er_rem_i_r(thr_rr, get_i(v), w);
		set_r(thr_rr);
		break;
	    case t_float:
		er_rem_d_r(thr_rr, get_d(v), w);
		set_r(thr_rr);
		break;
	    case t_mpz:
		er_rem_z_r(thr_rr, get_z(v), w);
		set_r(thr_rr);
		break;
	    case t_mpq:
		er_rem_q_r(thr_rr, get_q(v), w);
		set_r(thr_rr);
		break;
	    case t_mpr:
		er_rem_r_r(thr_rr, get_r(v), w);
		set_r(thr_rr);
		break;
	    default:
		eraise_exception(except_not_a_real_number);
	}
    }
    if (b)
	--thread_self->esp;
}

static void
vo_rem_vo_vo(vo_t *u, vo_t *v, vo_t *w, eint32_t m)
{
    for (; m; m--, u++, v++, w++)
	o_rem_o_o(u, v, w);
}

static void
o_rem_o_o(o_t *u, o_t *v, o_t *w)
{
    eint_t	i;

    switch (eobject_type(*v)) {
	case t_void:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    if (get_i(w) == 0)
			eraise_exception(except_floating_point_error);
		    set_i(0);
		    break;
		case t_mpz:	case t_mpq:
		    set_i(0);
		    break;
		case t_float:
		    /* FIXME result is zero or nan */
		    set_d(fmod(0.0, get_d(w)));
		    break;
		case t_mpr:
		    /* FIXME result is zero or nan */
		    er_rem_i_r(thr_rr, 0, get_r(w));
		    set_r(thr_rr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_int:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    set_i(get_i(v) % get_i(w));
		    break;
		case t_float:
		    set_d(fmod(get_i(v), get_d(w)));
		    break;
		case t_mpz:
		    if (ez_rem_i_z(thr_zr, get_i(v), get_z(w)))
			set_i(empz_get_i(thr_zr));
		    else
			set_z(thr_zr);
		    break;
		case t_mpq:
		    switch (eq_rem_i_q(thr_qr, get_i(v), get_q(w))) {
			case 1:
			    set_i(empz_get_i(thr_zr));
			    break;
			case 0:
			    set_q(thr_qr);
			    break;
			default:
			    set_z(thr_zr);
			    break;
		    }
		    break;
		case t_mpr:
		    er_rem_i_r(thr_rr, get_i(v), get_r(w));
		    set_r(thr_rr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_float:
	    switch (eobject_type(*w)) {
		case t_void:
		    /* FIXME only nan as result... */
		    set_d(fmod(get_d(v), 0.0));
		    break;
		case t_int:
		    set_d(fmod(get_d(v), get_i(w)));
		    break;
		case t_float:
		    set_d(fmod(get_d(v), get_d(w)));
		    break;
		case t_mpz:
		    set_d(fmod(get_d(v), mpz_get_d(get_z(w))));
		    break;
		case t_mpq:
		    set_d(fmod(get_d(v), mpq_get_d(get_q(w))));
		    break;
		case t_mpr:
		    er_rem_d_r(thr_rr, get_d(v), get_r(w));
		    set_r(thr_rr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_mpz:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    i = ez_rem_z_i(thr_zr, get_z(v), get_i(w));
		    if (i != MININT)	set_i(i);
		    else		set_z(thr_zr);
		    break;
		case t_float:
		    set_d(fmod(mpz_get_d(get_z(v)), get_d(w)));
		    break;
		case t_mpz:
		    if (ez_rem_z_z(thr_zr, get_z(v), get_z(w)))
			set_i(empz_get_i(thr_zr));
		    else
			set_z(thr_zr);
		    break;
		case t_mpq:
		    switch (eq_rem_z_q(thr_qr, get_z(v), get_q(w))) {
			case 1:
			    set_i(empz_get_i(thr_zr));
			    break;
			case 0:
			    set_q(thr_qr);
			    break;
			default:
			    set_z(thr_zr);
			    break;
		    }
		    break;
		case t_mpr:
		    er_rem_z_r(thr_rr, get_z(v), get_r(w));
		    set_r(thr_rr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_mpq:
	    switch (eobject_type(*w)) {
		case t_void:
		    eraise_exception(except_floating_point_error);
		case t_int:
		    switch (eq_rem_q_i(thr_qr, get_q(v), get_i(w))) {
			case 1:
			    set_i(empz_get_i(thr_zr));
			    break;
			case 0:
			    set_q(thr_qr);
			    break;
			default:
			    set_z(thr_zr);
			    break;
		    }
		    break;
		case t_float:
		    set_d(fmod(mpq_get_d(get_q(v)), get_d(w)));
		    break;
		case t_mpz:
		    switch (eq_rem_q_z(thr_qr, get_q(v), get_z(w))) {
			case 1:
			    set_i(empz_get_i(thr_zr));
			    break;
			case 0:
			    set_q(thr_qr);
			    break;
			default:
			    set_z(thr_zr);
			    break;
		    }
		    break;
		case t_mpq:
		    switch (eq_rem_q_q(thr_qr, get_q(v), get_q(w))) {
			case 1:
			    set_i(empz_get_i(thr_zr));
			    break;
			case 0:
			    set_q(thr_qr);
			    break;
			default:
			    set_z(thr_zr);
			    break;
		    }
		    break;
		case t_mpr:
		    er_rem_q_r(thr_rr, get_q(v), get_r(w));
		    set_r(thr_rr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	case t_mpr:
	    switch (eobject_type(*w)) {
		case t_void:
		    /* FIXME only nan as result */
		    er_rem_r_i(thr_rr, get_r(v), 0.0);
		    set_r(thr_rr);
		    break;
		case t_int:
		    er_rem_r_i(thr_rr, get_r(v), get_i(w));
		    set_r(thr_rr);
		    break;
		case t_float:
		    er_rem_r_d(thr_rr, get_r(v), get_d(w));
		    set_r(thr_rr);
		    break;
		case t_mpz:
		    er_rem_r_z(thr_rr, get_r(v), get_z(w));
		    set_r(thr_rr);
		    break;
		case t_mpq:
		    er_rem_r_q(thr_rr, get_r(v), get_q(w));
		    set_r(thr_rr);
		    break;
		case t_mpr:
		    er_rem_r_r(thr_rr, get_r(v), get_r(w));
		    set_r(thr_rr);
		    break;
		default:
		    eraise_exception(except_not_a_real_number);
	    }
	    break;
	default:
	    eraise_exception(except_not_a_real_number);
    }
}
#endif
